package com.pingtai.indent.action;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.springframework.security.access.AccessDeniedException;

import com.pingtai.filter.ChildUserFilter;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentMoneyTmp;
import com.pingtai.indent.entity.UserIndentSearch;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;

public class RenrenIndentAction {
	private UserBaseIndentType baseIndentType;
	private WeiboIndentService weiboIndentService;
	private String base;
	private UserManagerService userManagerService;
	private User curUser; 
	private Integer currentPage=1;
	private Integer pageNum;
	private PaginationSupport paginationSupport;
	private String current;
	private UserIndentSearch qe;
	private String queryString;
	private Float price;
	private UserIndent userIndent;
	private List indentNumberTmpList;
	private String typeRadio;
	
	public String getTypeRadio() {
		return typeRadio;
	}
	public void setTypeRadio(String typeRadio) {
		this.typeRadio = typeRadio;
	}
	public UserBaseIndentType getBaseIndentType() {
		return baseIndentType;
	}
	public void setBaseIndentType(UserBaseIndentType baseIndentType) {
		this.baseIndentType = baseIndentType;
	}
	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}
	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}
	public String getBase() {
		return base;
	}
	public void setBase(String base) {
		this.base = base;
	}
	public UserManagerService getUserManagerService() {
		return userManagerService;
	}
	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}
	public User getCurUser() {
		return curUser;
	}
	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public Integer getPageNum() {
		return pageNum;
	}
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	public PaginationSupport getPaginationSupport() {
		return paginationSupport;
	}
	public void setPaginationSupport(PaginationSupport paginationSupport) {
		this.paginationSupport = paginationSupport;
	}
	public String getCurrent() {
		return current;
	}
	public void setCurrent(String current) {
		this.current = current;
	}
	public UserIndentSearch getQe() {
		return qe;
	}
	public void setQe(UserIndentSearch qe) {
		this.qe = qe;
	}
	public String getQueryString() {
		return queryString;
	}
	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}
	public Float getPrice() {
		return price;
	}
	public void setPrice(Float price) {
		this.price = price;
	}
	public UserIndent getUserIndent() {
		return userIndent;
	}
	public void setUserIndent(UserIndent userIndent) {
		this.userIndent = userIndent;
	}
	public List getIndentNumberTmpList() {
		return indentNumberTmpList;
	}
	public void setIndentNumberTmpList(List indentNumberTmpList) {
		this.indentNumberTmpList = indentNumberTmpList;
	}
	
	public String renrenManageUI(){
		
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base));
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base));
		}
		current = "all";
	    
		return "success";
	}
	
	public String renrenManageUISearch(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base),qe);
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base),qe);
		}
		
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	
	public String toRenrenIndentAdd(){
		
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		
		if(baseIndentType.getEnable()==0){
			return "produceWeihu";
		}
		
		curUser = userManagerService.findCurrectUser();
		if(userManagerService.isRoot(curUser)){
			price = baseIndentType.getPrice();
		}else{
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			if(indentType==null){
				return "403";
			}else{
				indentType = weiboIndentService.updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		return "success";
	}
	
	public String addRenrenIndent(){
		//判断价钱是否合法
		User curUser = userManagerService.findCurrectUser();
		boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
		if(flag){
			
			
			
			userIndent.setCreateDate(new Date());
			userIndent.setNowNumber(userIndent.getStartNumber());
			User parentUser = userManagerService.findUserById(curUser.getParentUserId());
			userIndent.setParentUser(parentUser);
			userIndent.setState(0);
			
			baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
			if(userManagerService.isRoot(curUser)){
				price = baseIndentType.getPrice();
			}else{
				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
				if(indentType==null){
					return "403";
				}else{
					Float oldPrice = indentType.getPrice();
					indentType = weiboIndentService.updateIndentType(indentType);
					price = indentType.getPrice();
					if(!oldPrice.equals(price)){
						return "priceChange";
					}
				}
			}
			
			userIndent.setType(new Integer(base));
			userIndent.setUser(curUser);
			Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();;
			userIndent.setTotalPrice(t);
			userIndent.setPrice(price);
			
			//判余额是否充足
			Float money = curUser.getMoney();
			Float money2 = (float) Math.round(money*100/100);
			if(money2<t){
				return "moneyLow";
			}
			
			userIndent.setBasePrice(baseIndentType.getPrice());
			Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
			userIndent.setBaseTotalPrice(baseTotalPrice);
			
			userIndent.setBaseIndentType(baseIndentType);
			//保存订单表
			userIndent = weiboIndentService.saveUserIndent(userIndent);
			
			
			//所需金额保存到临时表
			UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
			userIndentMoneyTmp.setMoney(t);
			userIndentMoneyTmp.setUser(curUser);
			userIndentMoneyTmp.setUserIndent(userIndent);
			userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
			//从用户账户中的总金额减去订单总价 保存
			money = curUser.getMoney();
			money = money -t;
			curUser.setMoney(money);
			UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
			
			weiboIndentService.getIndentDao().evict(userIndent);
			weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
			weiboIndentService.getIndentDao().evict(userPay);
			
			
			//保存返利关系
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
			//userIndent2 = userIndent;
			userIndent=null;
			
		}else{
			return "add_indent_403";
		}
	
	return "success";
	}
	
	public String toRenrenIndentDetails(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		indentNumberTmpList = weiboIndentService.getIndentNumberTmp(userIndent);
		return "success";
	}
	
	public String toRenrenIndentRefund(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String renrenIndentRefund(){
		UserIndent userIndent1 = weiboIndentService.getUserIndentById(userIndent.getId());
		userIndent1.setRefundReason(userIndent.getRefundReason());
		User curUser = userManagerService.findCurrectUser();
		userIndent1.setState(userIndent.getState());
		userIndent1.setRefundDate(new Date());
		userIndent1.setRefundUser(curUser);
		weiboIndentService.saveUserIndent(userIndent1);
		UserPay userPay = new UserPay();
		
		User childUser = userIndent1.getUser();
		userPay.setChildUser(childUser);
		userPay.setUser(curUser);
		userPay.setCreateDate(new Date());
		userPay.setMoney(userIndent1.getTotalPrice());
		userPay.setType(1);
		userPay.setUserAllMoney(childUser.getMoney()+userIndent1.getTotalPrice());
		userPay.setUserIndent(userIndent1);
		userPay.setType2(4);
		userPay.setUserBaseIndent(userIndent1.getBaseIndentType());
		userPay.setIndentCreateUser(userIndent1.getUser());
		userManagerService.saveUserPay(userPay);
		childUser.setMoney(childUser.getMoney()+userIndent1.getTotalPrice());
		userManagerService.saveOrUpdateUser(childUser);
		baseIndentType = userIndent1.getBaseIndentType();
		return "success";
	}
	
	public String toRenrenIndentComplete(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String renrenIndentComplete(){
		int nowNumber = userIndent.getNowNumber();
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		baseIndentType = userIndent.getBaseIndentType();
		User completeUser = userManagerService.findCurrectUser();
		//调用返利方法
		try {
			userManagerService.userIndentRebate(userIndent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		userIndent.setState(2);
		userIndent.setNowNumber(nowNumber);
		userIndent.setEndDate(new Date());
		userIndent.setEndUser(completeUser);
		userIndent = weiboIndentService.saveUserIndent(userIndent);
		
		return "success";
	}
}
